package Yangtz.Cs.Reader.Controller.Login;

import Yangtz.Cs.Entity.ReaderBean;
import Yangtz.Cs.Service.ReaderService;
import Yangtz.Cs.Utils.DataTransmissionUtil;
import Yangtz.Cs.Utils.SetEncodingUtil;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码、响应方式和编码方式
        SetEncodingUtil.SetEncoding(request, response);

        //声明JSONObject对象
        JSONObject jsonObject1;     //用来接收android传来的数据
        JSONObject jsonObject2 = new JSONObject();     //用来传参


        //通过gainJsonFromAndroid(request)获取Android客户端发来的json数据，并将它解析
        jsonObject1 = DataTransmissionUtil.gainJsonFromAndroid(request);

        ReaderBean reader = new ReaderBean();

        //将从android传来的数据给到reader，用来判断数据库中是否存在该reader
        String rdID=jsonObject1.optString("rdID");
        String rdPwd=jsonObject1.optString("password");
        reader.setRdID(rdID);
        reader.setRdPWd(rdPwd);

        //创建服务层对象
        ReaderService readerService = new ReaderService();

        //调用该方法进行登陆识别,并把书籍的信息封装为json数组传给android
        readerService.LoginIdentify(reader,response);


    }


}

